home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Night Owl 9
/
Night Owl CD-ROM (NOPV9) (Night Owl Publisher) (1993).ISO
/
038a
/
satsfaxt.zip
/
CCPUTILS.EXE
/
CCSAVEX.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-01-30
|
16KB
|
382 lines
/*---------------------------------------------------------------------------*
* CCSAVEX.C *
*---------------------------------------------------------------------------*
* The program allows the user to move a file received from an Intel *
* Connection CoProcessor to a normal MS-DOS file. The file can be moved *
* to any pre-existing path on any disk drive. *
* *
* The following source code is intended to assist developers in *
* creating applications which support the DCA/Intel Communicating *
* Applications Specification Version 1.0A. It is provided free of charge *
* and on an as-is basis. THE IMPLIED WARRENTIES OF MERCHANTABILITY AND *
* FITNESS FOR A PARTICULAR PURPOSE ARE SPECIFICALLY EXCLUDED. This source *
* code may be modified, enhanced, copied and distributed with applications *
* that support CAS on a royalty free basis. *
*---------------------------------------------------------------------------*/
#include <stdio.h>
#include <dos.h>
#include <io.h>
#include <malloc.h>
#include <string.h>
#include "cas.h" /* Intel CAS header file. */
#include "parse.h" /* Command line parser header file. */
#include "util.h" /* CAS utilities header file. */
int MoveAll = FALSE; /* Save all received files. */
int Confirm = FALSE; /* Confirm before moving file. */
int Verbose = FALSE; /* List file names as they are moved. */
int Delete = FALSE; /* Delete event after saving files. */
int JobNo = 0; /* Job number to move. */
int Help = FALSE; /* Show help text. */
int RedirOut = FALSE; /* Redirect output to file. */
int FilesMoved = FALSE; /* TRUE if something was saved. */
ECF *TCFbuffer; /* Task Control File buffer. */
FTR *FTRbuffer; /* File Transfer Record buffer. */
FILE *ccout; /* Redirected stdout file. */
STABLE SwitchTable [] = /* Command line parser switch table. */
{
{ 'A', BOOL, &MoveAll },
{ 'a', BOOL, &MoveAll },
{ 'C', BOOL, &Confirm },
{ 'c', BOOL, &Confirm },
{ 'D', BOOL, &Delete },
{ 'd', BOOL, &Delete },
{ 'N', BOOL, &Verbose },
{ 'n', BOOL, &Verbose },
{ 'E', INT, &JobNo },
{ 'e', INT, &JobNo },
{ 'R', BOOL, &RedirOut },
{ 'r', BOOL, &RedirOut },
{ '?', BOOL, &Help }
};
#define TABLESIZE (sizeof(SwitchTable)/sizeof(STABLE))
char *DescTable[] = /* Help function description table. */
{
"CCSAVEX (V1.0) Saves (and deletes from the queue) a file transfer received\n",
"from a Connection CoProcessor.\n\n",
"The file can be saved to any existing directory on any disk drive (for\n",
"example, c:\\reports). If you specify the /A option, CCSAVEX will save ALL\n",
"file transfers in the receive queue to the specified directory.\n\n",
"Syntax: CCSAVEX /Exxxxx directory [option...]\n\n"
};
#define DESCSIZE (sizeof(DescTable)/sizeof(DescTable[0]))
char *HelpTable[] = /* Help function text table. */
{
" /A Move all received files to \"directory\". If the file\n",
" exists, you'll be prompted for overwrite permission.\n",
" /E<number> Save file transfer(s) associated with the event specified\n",
" by <number>.\n\n",
" /D Delete file transfer after saving files.\n\n",
" /C Confirm each file before saving.\n",
" /N Notify by listing files as they're saved.\n\n",
" /R Redirect output to file OUTPUT.CC.\n",
" /? Display help information.\n"
};
#define HELPSIZE (sizeof(HelpTable)/sizeof(HelpTable[0]))
/*---------------------------------------------------------------------------*
* GetTaskBuffer() *
*---------------------------------------------------------------------------*
* Get information about the event specified by Handle. The Task Control *
* File is opened and read. The FileHandle returned is a normal MS-DOS file *
* handle. *
*---------------------------------------------------------------------------*
* Parameters: int Handle - Event handle of task. *
* BYTE Queue - The queue (Task, Log or Receive) of event. *
* char *TCFbuffer - Uninitialized ECS structure, filled with *
* Task Control info on return. *
* int *FileHandle - Filled with DOS file handle on return. *
* Return: none *
*---------------------------------------------------------------------------*/
GetTaskBuffer(int Handle, BYTE Queue, char *TCFbuffer, int *FileHandle)
{
/* Let CAS open the Task Control File. Read the contents into TCFbuffer. */
if((*FileHandle = CASOpenFile(Handle,0,Queue)) > 0) {
read(*FileHandle, TCFbuffer, sizeof(ECF));
}
else
CASError(CASNOTCF, TRUE, -(*FileHandle));
}
/*---------------------------------------------------------------------------*
* GetFileTransferBuffer() *
*---------------------------------------------------------------------------*
* Get information about the File Transfer Record. Offset specifies the *
* location within the Task Control File of the FTR record. Usually, the *
* first FTR is at offset 383 in the TCF (if no cover text). In any case, *
* the offset of the first FTR is stored at offset 10 of the TCF. Each *
* subsequent FTR is 128 bytes after the first. *
*---------------------------------------------------------------------------*
* Parameters: long Offset - File offset to begin reading at. *
* char *FTRbuffer - Uninitialized FTR structure, filled with *
* File Transfer info on return. *
* int *FileHandle - DOS file handle obtained from a call to *
* CASOpenFile(). *
* Return: none *
*---------------------------------------------------------------------------*/
GetFileTransferBuffer(long Offset, char *FTRbuffer, int *FileHandle)
{
/* Read the File Transfer Record specified by Offset. */
lseek(*FileHandle, Offset, SEEK_SET);
read(*FileHandle, FTRbuffer, sizeof(FTR));
}
/*---------------------------------------------------------------------------*
* OkayToMove() *
*---------------------------------------------------------------------------*
* OkayToMove checks for existence of FileName. If the file does not exist, *
* the function returns TRUE. If it exists, CAS Move Received File will *
* fail, so the user is prompted for permission to overwrite. If permission *
* is granted, the file is removed and the function returns true. If the *
* removal fails or the user does not give permission, the function returns *
* false. *
*---------------------------------------------------------------------------*
* Parameters: char *FileName - Path and name of file to test. *
* Return: TRUE if it is okay to call CASMoveReceivedFile, FALSE *
* otherwise. *
*---------------------------------------------------------------------------*/
int OkToMove(char *FileName)
{
char answer[1]; /* Users response to confirmation quest. */
/* Check to see if file already exists. */
if(access(FileName, 0) == 0) {
/* Confirm overwrite on existing file. */
fprintf(stderr,"File %s already exists - overwrite? (y/n)", strupr(FileName));
gets(answer);
if(toupper(answer[0]) == 'Y') {
/* If we can't remove the existing file, don't try saving file. */
if(remove(FileName) == -1) {
CASError(CASDELJOB, FALSE, 0);
return(FALSE);
}
/* OK, save the file as planned. */
return(TRUE);
}
/* No permission to overwrite. */
else
return(FALSE);
}
/* File doesn't already exist - go ahead and save file. */
else {
/* User asked for confirmation first. */
if(Confirm) {
fprintf(stderr, "Save file %s? (y/n)", strupr(FileName));
gets(answer);
if(toupper(answer[0]) == 'Y')
return(TRUE);
else
return(FALSE);
}
/* Don't have to confirm - just do it. */
else
return(TRUE);
}
}
/*---------------------------------------------------------------------------*
* MoveFile() *
*---------------------------------------------------------------------------*
* Move all files associated with the event specified by EventHandle. The *
* files are moved to the path NewFilePath given on the command line. Each *
* file retains its original filename that was passed from the sending *
* machine. *
*---------------------------------------------------------------------------*
* Parameters: int EventHandle - Event handle of task owning the file to *
* be moved. *
* char *NewFilePath - Path name where file is to be saved. It *
* must not end with a filename or trialing '\'. The name *
* is taken from the FTR (i.e. the same name the file had *
* on the remote machine. *
* ECF *TCFbuffer - Filled in ECS structure. *
* FTR *FTRbuffer - Filled in FTR structure. *
* Return: none *
*---------------------------------------------------------------------------*/
MoveFile(int EventHandle, char *NewFilePath, ECF *TCFbuffer, FTR *FTRbuffer)
{
int Return; /* Return value from CAS functions. */
long Offset; /* Offset of FTR in task control file. */
int FileHandle; /* DOS file handle (from GetTaskBuffer). */
int x; /* Loop counter. */
char FileName[80]; /* Place to build the full path name. */
char NewFileName[80]; /* Temp path name. */
char SFileName[80]; /* Pointer to last '\' char in filename. */
GetTaskBuffer(EventHandle, RECEIVE_QUEUE, (char *)TCFbuffer, &FileHandle);
/* Only File Transfers are valid here. */
if(TCFbuffer->TransferType != FILE_TRANSFER)
CASError (CASNOTFT, TRUE, 0);
/* The first FTR is at the offset stored at FTROffset in the TCF.
* This will vary depending on if cover text was sent. */
Offset = TCFbuffer->FTROffset;
/* For each additional received file, build a path name and move it. */
for(x = 1; x <= TCFbuffer->FileCount; x++) {
/* Get information about the file that was transferred. */
GetFileTransferBuffer(Offset, (char *)FTRbuffer, &FileHandle);
/* Build the full path name to the new file. */
strcpy(FileName, NewFilePath);
strcpy(SFileName, strrchr(FTRbuffer->FileName, '\\'));
if(SFileName[0] == '\0') {
if(FileName[strlen(FileName) - 1] != '\\') {
strcpy(NewFileName, "\\");
strcat(NewFileName, FTRbuffer->FileName);
}
else
strcpy(NewFileName, FTRbuffer->FileName);
strcat(FileName, NewFileName);
}
else
strcat(FileName, SFileName);
/* Clear temp file name. */
memset(NewFileName, 0, 80);
/* Now check to see if the file has already been saved. */
if(FTRbuffer->FileStatus == MOVED) {
fprintf(stderr, "%s : ", strupr(FileName));
CASError(CASPRIORMOVE, FALSE, 0);
}
else {
/* Check if OK to move - if so, move it. */
if(OkToMove(FileName)) {
if(Verbose)
fprintf(stderr, "Saving %s\n", strupr(FileName));
if((Return = CASMoveReceivedFile(EventHandle, x, FileName)) != SUCCESS)
CASError(CASMOVE, FALSE, -Return);
else
FilesMoved += 1;
}
}
/* The next FTR is 128 bytes after the previous. */
Offset = Offset + 128;
}
close(FileHandle);
if(Delete)
CASDeleteFile(EventHandle, 0, LOG_QUEUE);
}
/*---------------------------------------------------------------------------*
* SaveAllFiles() *
*---------------------------------------------------------------------------*
* Search the Receive queue for files to save. When complete, all files in *
* the receive queue will have been moved to the directory specified by the *
* NewFilePath parameter. *
*---------------------------------------------------------------------------*
* Paramters: char *NewFilePath - Directory and drive where files are to *
* be moved to. *
* ECF *TCFbuffer - Uninitialized ECF structure. *
* FTR *FTRbuffer - Uninitialized FTR structure. *
* Return: none *
*---------------------------------------------------------------------------*/
SaveAllFiles(char *NewFilePath, ECF *TCFbuffer, FTR *FTRbuffer)
{
int FileHandle; /* Open ECF file handle. */
int EventHandle; /* Event handle. */
int done = FALSE; /* Queue processing flag. */
if((EventHandle = CASFindFirst(COMPLETED,SEARCH_FORWARD,RECEIVE_QUEUE)) < 0)
done = TRUE;
while(done == FALSE) {
GetTaskBuffer(EventHandle, RECEIVE_QUEUE, (char *)TCFbuffer, &FileHandle);
if(TCFbuffer->TransferType == FILE_TRANSFER) {
close(FileHandle);
MoveFile(EventHandle, NewFilePath, TCFbuffer, FTRbuffer);
}
if ((EventHandle = CASFindNext(RECEIVE_QUEUE)) < 0)
done = TRUE;
}
}
/*---------------------------------------------------------------------------*
* Main *
*---------------------------------------------------------------------------*
* Move all files from the specified event to another directory, saving *
* them as normal DOS files. If the /A switch is given on the command line *
* then all files (from all events) in the Receive queue are saved to that *
* directory. *
*---------------------------------------------------------------------------*
* Return: The DOS exit code is 0 if no errors are encountered, 1 for *
* commandline syntax errors, 2 for CCAM error, and 3 for DOS *
* errors. *
*---------------------------------------------------------------------------*/
main(int argc, char **argv)
{
int result; /* DOS & CAS function return values. */
char Name[80]; /* Path name from command line. */
argc = ParseCommand(argc, argv, SwitchTable, TABLESIZE);
/* If help is wanted, print info and exit (with 0 exit code). */
if(Help || (argc == 1))
CASHelp(DescTable, DESCSIZE, HelpTable, HELPSIZE);
/* Insure that the Resident Scheduler is installed. */
if((result = CASGetInstalledState()) != INSTALLED) {
if( result == NOTiOK )
CASError(CASNOHWO, TRUE, 0);
else
CASError(CASNOHWN, TRUE, 0);
}
/* Redirect stdout to a file. */
if(RedirOut) {
if((ccout = fopen("OUTPUT.CC", "w")) == NULL)
CASError(CASOPENTEMP, TRUE, 0);
if(-1 == dup2(fileno(ccout), 1))
CASError(CASBADREDIR, TRUE, 0);
}
/* Check to make sure that a (one only) path name was specified. */
if(argc < 2)
CASError(CASARGPATH, TRUE, 0);
else if(argc > 2)
CASError(CASARGMULTPATH, TRUE, 0);
/* Get buffer for Task information. */
TCFbuffer = (ECF *)malloc(sizeof(ECF));
if(TCFbuffer == NULL)
CASError(CASNOMEM, TRUE, 0);
/* Get buffer for File Transfer information. */
FTRbuffer = (FTR *)malloc(sizeof(FTR));
if(FTRbuffer == NULL)
CASError(CASNOMEM, TRUE, 0);
/* Check for the existance of the specified path name, if ok, then
* move it. */
++argv;
strcpy(Name, *argv);
if(access(Name, 0) == -1)
CASError(CASNOPATH, TRUE, 0);
else if(MoveAll)
SaveAllFiles(Name, TCFbuffer, FTRbuffer);
else
MoveFile(JobNo, Name, TCFbuffer, FTRbuffer);
/* If nothing was saved - and we didn't exit due to errors, let the user
know what happened. */
if(!FilesMoved)
CASError(CASNOMOVE, TRUE, 0);
else
fprintf(stderr, " %d files saved.\n", FilesMoved);
exit(0);
}